package com.zking.controller.dwm;

import com.zking.service.dwm.CartService_dwm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

@Controller
@RequestMapping(value = "/cart")
public class CartController_dwm {

    @Autowired
    private CartService_dwm cartService_dwm;

    @RequestMapping(value = "/select")
    public String selec(HttpServletRequest request){
        String name = request.getParameter("name");
        request.setAttribute("cartName",cartService_dwm.selectCartName(name));
        request.setAttribute("carts",cartService_dwm.selectCartUserName(name));
        return "dwm/cart";
    }

    @RequestMapping(value = "showPic")
    private void showPic(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String pic = request.getParameter("pic");
        File file = new File(pic);
        //将图片的字节写入到页面
        //jsp中的out写入东西
        OutputStream out = response.getOutputStream();
        InputStream in = new BufferedInputStream(new FileInputStream(file));
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = in.read(b)) != -1) {
            out.write(b, 0, len);
        }
        out.flush();
        out.close();
        in.close();
        response.flushBuffer();//刷新缓存
    }

    @RequestMapping(value = "/jie")
    @ResponseBody
    public int jie(HttpServletRequest request){
        String id = request.getParameter("id");
        System.out.println(id);
        int unma = Integer.parseInt(request.getParameter("unma"));
        if (unma==1){
            return 1;
        }else{
            int unme = unma-1;
            cartService_dwm.updateCartNumber(unme,id);
            return unme;
        }

    }
    @RequestMapping(value = "/jia")
    @ResponseBody
    public int jia(HttpServletRequest request){
        String id = request.getParameter("id");
        System.out.println(id);
        int unma = Integer.parseInt(request.getParameter("unma"));
        if (unma==10){
            return 10;
        }else{
            int unme = unma+1;
            cartService_dwm.updateCartNumber(unme,id);
            return unme;
        }

    }

    @RequestMapping(value = "/updateCartState")
    public String updateCartState(HttpServletRequest request){
        String id = request.getParameter("id");
        int state = Integer.parseInt(request.getParameter("state"));
        cartService_dwm.updateCartState(state,id);
        String name = request.getParameter("name");
        request.setAttribute("cartName",cartService_dwm.selectCartName(name));
        request.setAttribute("carts",cartService_dwm.selectCartUserName(name));
        return "dwm/cart";
    }

    @RequestMapping(value = "/addUpdate")
    public String addUpdate(HttpServletRequest request){
        String[] ids = request.getParameter("id").split(",");
         for (int i=0;i<ids.length;i++){
             cartService_dwm.updateCartState(0,ids[i]);
         }
        String name = request.getParameter("name");
        request.setAttribute("cartName",cartService_dwm.selectCartName(name));
        request.setAttribute("carts",cartService_dwm.selectCartUserName(name));
        return "dwm/cart";
    }
}
